// 将数字转为中文
function NoToChinese(num) {
  if (!/^\d*(\.\d*)?$/.test(num)) {
    alert('Number is wrong!')
    return 'Number is wrong!'
  }
  var AA = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  var BB = ['', '十', '百', '千', '万', '亿', '点', '']
  var a = ('' + num).replace(/(^0*)/g, '').split('.')
  var k = 0
  var re = ''
  for (let i = a[0].length - 1; i >= 0; i--) {
    switch (k) {
      case 0:
        re = BB[7] + re
        break
      case 4:
        if (
          !new RegExp('0{4}\\d{' + (a[0].length - i - 1) + '}$').test(a[0])
        ) {
          re = BB[4] + re
        }
        break
      case 8:
        re = BB[5] + re
        BB[7] = BB[5]
        k = 0
        break
    }
    if (
      k % 4 === 2 &&
      a[0].charAt(i + 2) !== 0 &&
      a[0].charAt(i + 1) === 0
    ) {
      re = AA[0] + re
    }
    if (a[0].charAt(i) !== 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re
    k++
  }
  if (a.length > 1) {
    // 加上小数部分(如果有小数部分)
    re += BB[6]
    for (let i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)]
  }
  return re
}
export function questionnaireDataSet(data, list) {
  var answer = {}// 答案对象，以id为键名{题目id:{选项1id:{},选项2id:{},选项3id:{}}}
  const childIndex = {}
  list.sort((a, b) => { return a.serialNumber - b.serialNumber })
  list.forEach((c, i) => {
    c.show = false
    c.chapter_index_text = NoToChinese(i + 1)
    let topic_index = 0
    c.questionnaireQuestionDTOList.sort((a, b) => { return a.displaySerialNumber - b.displaySerialNumber }).forEach((e, j) => {
      e.chaperId = c.id
      if (e.prefixQuestionId) {
        e.topic_index = topic_index
        if (childIndex[e.prefixQuestionId]) {
          // 有前置条件的，设置附属序号：例：1.1，1.2；点后面的序号(c_index)
          e.c_index = childIndex[e.prefixQuestionId] + 1
        } else {
          childIndex[e.prefixQuestionId] = e.c_index = 1
        }
      } else {
        e.topic_index = topic_index = topic_index + 1
      }
      var itemAnswer = {}
      e.questionOptionDTOList.forEach(option => {
        itemAnswer[option.id] = {
          isSel: false, // 是否选中
          deducted: false, // 是否为扣分选项，不是则为加分项
          enterContent: '', // 当前项输入内容
          evaluate: '', // 评语
          appeal: '' // 申诉备注
        } // 选中的选项
      })
      answer[e.id] = itemAnswer
      e.questionnaireId = data.id
    })
  })

  return { answer, questionnaire: data }
}
